package com.xad.service.impl;

import com.mysql.cj.xdevapi.DbDoc;
import com.xad.entity.User;
import com.xad.entity.UserExample;
import com.xad.mapper.UserMapper;
import com.xad.service.UserService;
import com.xad.util.DBUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.defaults.DefaultSqlSession;

import java.util.List;

/**
 * @Version :
 * @PROJECT_NAME: mybatis
 * @PACKAGE_NAME:com.xad.service.impl
 * @NAME: UserServiceImpl
 * @author:xuandian
 * @DATE: 2022/6/29 15:36
 * @description: 业务接口实现
 */
public class UserServiceImpl implements UserService {
//    public static SqlSession sqlSession = DBUtil.getSqlSession();
//    public static UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    @Override
    public int insertUser(User user) {
         SqlSession sqlSession = DBUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        return userMapper.insert(user);
    }

    @Override
    public int deleteUser(Integer id) {
        SqlSession sqlSession = DBUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateUser(User user) {
        SqlSession sqlSession = DBUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public User user(Integer id) {
        SqlSession sqlSession = DBUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> selectAll(UserExample userExample) {
        SqlSession sqlSession = DBUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        return userMapper.selectByExample(userExample);
    }
}
